package base.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * 引入比较器，方便Collections进行比较
 * @author admin
 *
 */
public class ComparatorTest {
	  public static void main(String[] args) {
	        Random r =new Random();
	        List<Hero> heros = new ArrayList<Hero>();
	        for (int i = 0; i < 10; i++) {
	            //通过随机值实例化hero的hp和damage
	            heros.add(new Hero("hero "+ i, r.nextInt(100), r.nextInt(100)));
	        }
	        System.out.println("初始化后的集合：");
	        System.out.println(heros);
	        //引入比较器，这是一个内部类。
	        Comparator<Hero> c = new Comparator<Hero>(){

				@Override
				public int compare(Hero h1, Hero h2) {
					if(h1.hp>=h2.hp){
						return 1;
					}else{
						return -1;
					}
					
				}
	        	
	        };
	        Collections.sort(heros, c);
	        System.out.println("按照血量排序后的集合：");
	        System.out.println(heros);

	    }

}
